<template>
  <div class="app-container">
    <el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="100px">
      <el-row gutter="15">
        <el-col :span="12">
          <el-form-item label="课程名称" prop="name">
            <el-input v-model="formData.name" type="text" placeholder="请输入课程名称" clearable :style="{ width: '100%' }"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课程类型" prop="type">
            <el-select v-model="formData.type" placeholder="请选择课程类型" clearable :style="{ width: '100%' }">
              <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row gutter="15">
        <el-col :span="12">
          <el-form-item label="是否为必学" prop="istrue">
            <el-radio-group v-model="formData.istrue" size="default">
              <el-radio v-for="(item, index) in istrueOptions" :key="index" :value="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="级联选择" prop="field106">
            <el-cascader v-model="formData.field106" :options="field106Options" :props="field106Props" :style="{ width: '100%' }" placeholder="请选择级联选择" clearable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="多行文本" prop="field107">
        <el-input v-model="formData.field107" type="textarea" placeholder="请输入多行文本" :autosize="{ minRows: 4, maxRows: 4 }" :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="上传" prop="field108" required>
        <el-upload ref="field108" :file-list="field108fileList" :action="field108Action" :before-upload="field108BeforeUpload">
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-row gutter="15">
        <el-col :span="12">
          <el-form-item label="计数器" prop="field109">
            <el-input-number v-model="formData.field109" placeholder="计数器"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评分" prop="field111">
            <el-rate v-model="formData.field111"></el-rate>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import { reactive, ref, toRefs } from "vue";

const formRef = ref();
const data = reactive({
  formData: {
    name: undefined,
    type: undefined,
    istrue: 0,
    field106: [],
    field107: undefined,
    field108: null,
    field109: 1,
    field111: 0,
  },
  rules: {
    name: [
      {
        required: true,
        message: "请输入课程名称",
        trigger: "blur",
      },
    ],
    type: [
      {
        required: true,
        message: "请选择课程类型",
        trigger: "change",
      },
    ],
    istrue: [],
    field106: [
      {
        required: true,
        type: "array",
        message: "请至少选择一个field106",
        trigger: "change",
      },
    ],
    field107: [
      {
        required: true,
        message: "请输入多行文本",
        trigger: "blur",
      },
    ],
    field109: [
      {
        required: true,
        message: "计数器",
        trigger: "blur",
      },
    ],
    field111: [
      {
        required: true,
        message: "评分不能为空",
        trigger: "change",
      },
    ],
  },
});
const { formData, rules } = toRefs(data);
const typeOptions = ref([
  {
    label: "课程",
    value: "1",
  },
]);
const istrueOptions = ref([
  {
    label: "是",
    value: 1,
  },
  {
    label: "否",
    value: 2,
  },
]);
const field106Options = ref([]);
// 上传请求路径
const field108Action = ref("https://jsonplaceholder.typicode.com/posts/");
// 上传文件列表
const field108fileList = ref([]);
// props设置
const field106Props = ref({
  multiple: false,
});

function getField106Options() {
  // TODO 发起请求获取数据
  field106Options.value;
}
/**
 * @name: 上传之前的文件判断
 * @description: 上传之前的文件判断，判断文件大小文件类型等
 * @param {*} file
 * @return {*}
 */
const field108BeforeUpload = (file: any): any => {
  let isRightSize = file.size / 1024 / 1024 < 2;
  if (!isRightSize) {
    ElMessage.error("文件大小超过 2MB");
  }
  return isRightSize;
};
/**
 * @name: 表单提交
 * @description: 表单提交方法
 * @return {*}
 */
const submitForm = (): any => {
  formRef.value.validate((valid: any) => {
    if (!valid) return;
    // TODO 提交表单
  });
};
/**
 * @name: 表单重置
 * @description: 表单重置方法
 * @return {*}
 */
const resetForm = (): any => {
  formRef.value.resetFields();
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

.el-rate {
  display: inline-block;
  vertical-align: text-top;
}
</style>
